package ofc4j.util;

import ofc4j.model.elements.PieChart;
import ofc4j.model.elements.PieChart.Slice;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.path.PathTrackingWriter;

public class PieChartSliceConverter implements Converter {
    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert(Class c) {
        return PieChart.Slice.class.isAssignableFrom(c);
    }

    @Override
    public void marshal(Object o, HierarchicalStreamWriter hsw, MarshallingContext ctxt) {
        PathTrackingWriter writer = (PathTrackingWriter) hsw;
        Slice slice = (Slice) o;
        writer.startNode("value", slice.getValue().getClass());
        writer.setValue(slice.getValue().toString());
        writer.endNode();
        String text = slice.getText();
        if (text != null) {
            writer.startNode("text", String.class);
            writer.setValue(text);
            writer.endNode();
        }
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {
        return null;
    }
}
